$.request = (function () { 
    var apiMap = {}; 
    function request(queryStr) { 
        var api = {}; 
        if (apiMap[queryStr]) { return apiMap[queryStr]; } 
        api.queryString = (function () { 
            var urlParams = {}; 
            var e, 
            d = function (s) { return decodeURIComponent(s.replace(/\+/g, " ")); }, 
            q = queryStr.substring(queryStr.indexOf('?') + 1), 
            r = /([^&=]+)=?([^&]*)/g; 
            while (e = r.exec(q))     urlParams[d(e[1])] = d(e[2]);
            return urlParams; 
        })(); 
        api.getUrl = function () { 
            var url = queryStr.substring(0, queryStr.indexOf('?') + 1); 
            for (var p in api.queryString) { url += p + '=' + api.queryString[p] + "&";        } 
            if (url.lastIndexOf('&') == url.length - 1) { return url.substring(0, url.lastIndexOf('&')); } 
            return url; 
        } 
        apiMap[queryStr] = api; 
        return api; 
    } 
    $.extend(request, request(window.location.href)); 
    return request; 
})();
/***************************************************************************************************/
let userInfo = {};
let code = '';// 微信官方提供的页面code

// 读取url参数并填写到页面中
function init(){
    userInfo.mill = $.request.queryString["mill"];
    userInfo.clientId = $.request.queryString["clientId"];
    userInfo.clientSid = $.request.queryString["clientSid"];
    userInfo.clientName = $.request.queryString["clientName"];
    userInfo.nickName = $.request.queryString["nickName"];
    userInfo.openId = $.request.queryString["openId"];
    userInfo.realName = $.request.queryString["realName"];
    userInfo.code = $.request.queryString["code"];

    $(`#mill`).html(`染厂：${userInfo.mill}`);
    $(`#clientName`).html(`客户名称：${userInfo.clientName}`);
    $(`#clientId`).html(`客户云端主键：${userInfo.clientId}`);
    $(`#clientSid`).html(`客户ERP编号：${userInfo.clientSid}`);
    $(`#openId`).html(`openId：${userInfo.openId}`);
    $(`#nickName`).html(`昵称：${userInfo.nickName}`);
    
    if(!userInfo.realName) userInfo.realName = userInfo.nickName
    $("#realName").val(userInfo.realName);
}

// 设置企业云权限
function setEnterprise(){
    const url = "https://www.xdfznh.club/wxpadbe/user/setEnterpriseCloud";
    doPost(url);
}

// 设置客户云权限
function setClient(){
    const url = "https://www.xdfznh.club/wxpadbe/user/setClientCloud";
    doPost(url);
}

function doPost(url){
    const realName = document.getElementById("realName").value
    userInfo.realName = realName;
    console.log('设置权限的URL和参数依次是：', url, userInfo);

    $.ajax({
        url : url,
        data: JSON.stringify(userInfo),
        type : "POST",
        contentType: "application/json;charset=UTF-8",
        async: true,
        success : function(res) {
            if(!res) {
                alert('后台没有响应！');
                return
            }

            const resCode = parseInt(res.code);
            if(resCode !== 1){
                alert(res.msg);
                return
            }

            $(`#result`).html(`设置完毕！`);
        },
        error : function(e){
            console.log(e.status);
            console.log(e.responseText);
            alert(e.responseText);
        },
        complete: function () {}
    })
}

// 页面初始化后执行
(function(){
  init();// 初始化页面
})();
